using System;
using System.Collections.Generic;
using System.Windows.Forms;
using TddGenerator.Core.Services;
using TddGenerator.Core.Services.Impl;
using TddGenerator.Ui;

namespace TddGenerator.Addin
{
    public partial class SelectClassName : Form, IInterfaceUnderTestSelectorView
    {
        private readonly IDefaultSettingsRepository defaultSettingsRepository;
        private readonly ProjectRepository repository;

        public SelectClassName()
        {
            InitializeComponent();
        }

        public SelectClassName(ProjectRepository repository, IDefaultSettingsRepository defaultSettingsRepository)
            : this()
        {
            this.repository = repository;
            this.defaultSettingsRepository = defaultSettingsRepository;
        }

        #region IInterfaceUnderTestSelectorView Members

        void IInterfaceUnderTestSelectorView.Show()
        {
            ShowDialog();
        }

        public void SetClassProjectDropdown(IEnumerable<string> projects)
        {
            ClassProjectDropDown.DataSource = projects;
        }

        public void SetTestProjectDropdown(IEnumerable<string> projects)
        {
            testClassProjectDropDown.DataSource = projects;
        }

        public void DisplayErrorMessage(string Message)
        {
            MessageBox.Show(Message);
        }

        public event EventHandler EnterPressed;
        public event EventHandler EscPressed;
        public event EventHandler OkButtonClicked;
        public event EventHandler CancelButtonClicked;

        public string ClassProjectName
        {
            get { return ClassProjectDropDown.Text; }
        }

        public string TestProjectName
        {
            get { return testClassProjectDropDown.Text; }
        }

        public IList<string> FolderName
        {
            get { return textBox1.Text.Split('\\'); }
        }

        public string ClassName
        {
            get { return textBox2.Text; }
        }

        public void SetFolder(string folderName)
        {
            textBox1.Text = folderName;
        }

        public void SetClassProjectText(string project)
        {
            ClassProjectDropDown.Text = project;
        }

        public void SetTestProjectText(string project)
        {
            testClassProjectDropDown.Text = project;
        }

        #endregion

        //public ClassToCreate Display()
        //{
        //    this.ClassProjectDropDown.DataSource = repository.GetProjectNames();

        //    this.testClassProjectDropDown.DataSource = repository.GetProjectNames();

        //    DefaultSettings defaultSettings = defaultSettingsRepository.Get();
        //    if (defaultSettings != null)
        //    {
        //        this.ClassProjectDropDown.SelectedIndex = this.ClassProjectDropDown.Items.IndexOf(defaultSettings.ClassProject);
        //        this.testClassProjectDropDown.SelectedIndex = testClassProjectDropDown.Items.IndexOf(defaultSettings.TestProject);
        //        this.textBox1.Text = defaultSettings.Namespace;
        //        this.textBox2.Select();
        //    }
        //    else
        //        defaultSettings= new DefaultSettings();

        //    DialogResult dialogResult = this.ShowDialog();
        //    ClassToCreate classToCreate = null;
        //    if (dialogResult == System.Windows.Forms.DialogResult.OK)
        //    {
        //        classToCreate=new ClassToCreate();
        //        classToCreate.ClassName = this.textBox2.Text;
        //        classToCreate.FolderName = this.textBox1.Text;
        //        classToCreate.ClassProject = this.ClassProjectDropDown.Text;
        //        classToCreate.TestProject = this.testClassProjectDropDown.Text;

        //        defaultSettings.ClassProject = classToCreate.ClassProject;
        //        defaultSettings.TestProject = classToCreate.TestProject;
        //        defaultSettings.Namespace = classToCreate.FolderName;

        //        defaultSettingsRepository.Save(defaultSettings);
        //    }
        //    return classToCreate;
        //}


        private void button1_Click(object sender, EventArgs e)
        {
            OkButtonClicked(sender, e);
            //this.DialogResult = System.Windows.Forms.DialogResult.OK;
            //this.Hide();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            CancelButtonClicked(sender, e);
        }

        private void SelectClassName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                EnterPressed(sender, e);
            }
            else if (e.KeyChar == 27)
            {
                EscPressed(sender, e);
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            OkButtonClicked(sender, e);
        }
    }
}